<?php

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\SabaTrait;

class saba extends Game
{
    use SabaTrait;


    public $timezone = 'GMT-4';
    private $platName = 'saba';


    const API_CREATEMEMBER = '/api/CreateMember';
    const API_LOGIN = '/api/Login';
    const API_CHECKUSERBALANCE = '/api/CheckUserBalance';
    const API_FUNDTRANSFER = '/api/FundTransfer';
    const API_CHECKFUNDTRANSFER = '/api/CheckFundTransfer';
    const API_GETSPORTBETTINGDETAIL = '/api/GetSportBettingDetail';
    const API_KICKUSER = '/api/KickUser';

    public function __construct()
    {
        $this->adds = [
            'main_game_plat_code' => $this->platName,
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    private function login($accountUserName)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $accountUserName
        ];

        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_LOGIN . '?' . str_replace('%3A', '%3a', http_build_query($param))));
        $url = $this->gameUrl . self::API_LOGIN . '?' . $securityToken . '&' . http_build_query($param);
        $output = $this->request('GET', $url);
        $output = json_decode($output, true);
        if ($output['error_code'] === 0) {
            return $output['sessionToken'];
        } else {
            return false;
        }
    }

    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $accountUserName,
            'OddsType' => $this->oddstype,
            'MaxTransfer' => $this->maxtransfer,
            'MinTransfer' => $this->mintransfer
        ];

        $temp = str_replace('%3A', '%3a', http_build_query($param));
        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_CREATEMEMBER . '?' . $temp));
        $url = $this->gameUrl . self::API_CREATEMEMBER . '?' . $securityToken . '&' . http_build_query($param);
        $output = $this->request('GET', $url);
        \Log::info('沙巴请求返回的值是'.$output);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['error_code']) && $outputarr['error_code'] == 0) {
            return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }

    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $accountUserName
        ];

        $temp = str_replace('%3A', '%3a', http_build_query($param));
        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_CHECKUSERBALANCE . '?' . $temp));
        $url = $this->gameUrl . self::API_CHECKUSERBALANCE . '?' . $securityToken . '&' . http_build_query($param);

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['error_code']) && $outputarr['error_code'] == 0) {
            return ['success' => true, 'data' => ['balance' => $outputarr['Data'][0]['balance']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败:' . $outputarr['message']];
        }
    }

    //转入游戏

    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $accountUserName,
            'amount' => $price,
            'OpTransId' => $transferid,
            'direction' => 1
        ];

        $temp = str_replace('%3A', '%3a', http_build_query($param));
        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_FUNDTRANSFER . '?' . $temp));
        $url = $this->gameUrl . self::API_FUNDTRANSFER . '?' . $securityToken . '&' . http_build_query($param);

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['error_code'])) {
            if ($outputarr['error_code'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败:' . $outputarr['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $accountUserName,
            'amount' => $price,
            'OpTransId' => $transferid,
            'direction' => 0
        ];

        $temp = str_replace('%3A', '%3a', http_build_query($param));
        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_FUNDTRANSFER . '?' . $temp));
        $url = $this->gameUrl . self::API_FUNDTRANSFER . '?' . $securityToken . '&' . http_build_query($param);

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['error_code'])) {
            if ($outputarr['error_code'] == 0) {
                return ['success' => true, 'data' => ['transferid' => $transferid, 'transfer_state' => 1], 'message' => ''];
            } else {
                return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败:' . $outputarr['message']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '转帐未知'];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $request->accountUserName,
            'OpTransId' => $request->transferid
        ];

        $temp = str_replace('%3A', '%3a', http_build_query($param));
        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_CHECKFUNDTRANSFER . '?' . $temp));
        $url = $this->gameUrl . self::API_CHECKFUNDTRANSFER . '?' . $securityToken . '&' . http_build_query($param);

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['error_code'])) {
            if ($outputarr['error_code'] == 0 ) {
                if(array_key_exists('status',$outputarr['error_code']['Data'])){
                    if($outputarr['error_code']['Data']['status']==0){
                        return ['success' => true, 'data' => ['transfer_state' => 1], 'message' => ''];
                    }else{
                        return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '订单检查失败:' . $outputarr['message']];
                    }
                }else{
                    return ['success' => false, 'data' => ['transfer_state' => -1, ],'message' => '订单检查失败:' . $outputarr['message']];
                }
            } else{
                return ['success' => false, 'data' => ['transfer_state' => -1, ],'message' => '订单检查失败:' . $outputarr['message']];
            }
        } else {
            return ['success' => true, 'data' => ['transfer_state' => -1], 'message' => '平台接口请求错误'];
        }
    }

    //踢线
    public function curlKick($request)
    {
        $param = [
            'OpCode' => $this->opcode,
            'PlayerName' => $request->accountUserName,

        ];

        $temp = str_replace('%3A', '%3a', http_build_query($param));
        $securityToken = 'SecurityToken=' . strtoupper(md5($this->security . self::API_KICKUSER . '?' . $temp));
        $url = $this->gameUrl . self::API_KICKUSER . '?' . $securityToken . '&' . http_build_query($param);

        $output = $this->request('GET', $url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['error_code'])) {
            if ($outputarr['error_code'] == 0) {
                return ['success' => true, 'data' => [], 'message' => ''];
            } else {
                return ['success' => false, 'data' => [], 'message' => '踢线失败:' . $outputarr['message']];
            }
        } else {
            return ['success' => true, 'data' => [], 'message' => '平台接口请求错误'];
        }
    }

    //进入游戏
    public function curlJoinGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $api = $_SERVER['HTTP_HOST'];
        if (strpos($_SERVER['HTTP_HOST'], 'www.') !== false) {
            $api = str_replace('www.', '', $api);
        }
        $token = $this->login($accountUserName);
        $param = [
            'g' => $token,
            'lang' => 'cs',
            'OType' => '2'
        ];
        setcookie("g", $param['g'], time() + 3600, '/', '.' . $api);

        return ['success' => true, 'data' => ['url' => $this->PCGame . '?' . http_build_query($param), 'method' => 1], 'message' => ''];
    }


    public function curlJoinMobileGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $api = $_SERVER['HTTP_HOST'];
        if (strpos($_SERVER['HTTP_HOST'], 'www.') !== false) {
            $api = str_replace('www.', '', $api);
        }
        $token = $this->login($accountUserName);
        $param = [
            'g' => $token,
            'lang' => 'cs',
            'OType' => '2'
        ];
        setcookie("g", $param['g'], time() + 3600, '/', '.' . $api);
        return ['success' => true, 'data' => ['url' => $this->mobileGame . '?lang=cs&st=' . $param['g'], 'method' => 1], 'message' => ''];
    }


   //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $rMode = 1;
        if ($rMode) {
            $starttime = $startTime->timezone('GMT+8')->toDateTimeString();
            $endtime = $endTime->timezone('GMT+8')->toDateTimeString();
        } else {
            $starttime = $startTime->toDateTimeString();
            $endtime = $endTime->toDateTimeString();
        }

        $data = [
            'OpCode' => $this->opcode,
            'StartTime' => $starttime,
            'EndTime' => $endtime,
            'rMode' => $rMode
        ];
        $temp = str_replace('%3A', '%3a', http_build_query($data));
        $securityToken = strtoupper(md5($this->security . self::API_GETSPORTBETTINGDETAIL . '?' . $temp));
        $params = array_merge($data, ['SecurityToken' => $securityToken]);
        $url = $this->gameUrl . self::API_GETSPORTBETTINGDETAIL;
        $result = $this->request('GET', $url, $params);
        $output = json_decode($result, true);

        if (isset($output['error_code']) && $output['error_code'] == 0) {
            return $output['Data'];
        }

    }
}